package day17.tarena.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class MenuTest {
	private JFrame frame;
	private JTextArea area;
	
	private JMenu file,edit,view;
	
	public MenuTest(){
		frame=new JFrame("记事本v1.0");
		area=new JTextArea(20,40);
		file=new JMenu("文件");
		edit=new JMenu("编辑");
		view=new JMenu("视图");
	}
	
	private void init(){
		JMenuItem item;
		file.add(new JMenuItem("新建..."));
		file.add(item=new JMenuItem("打开..."));
		item.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				JFileChooser jfc=new JFileChooser();
				jfc.showOpenDialog(frame);
				File file=jfc.getSelectedFile();
				area.setText(file.getName());
			}
			
		});
		file.addSeparator();
		file.add(new JMenuItem("保存"));
		file.add(new JMenuItem("另存为..."));
		file.addSeparator();
		file.add(new JMenuItem("打印..."));
		file.addSeparator();
		file.add(new JMenuItem("退出"));
		
		edit.add(new JMenuItem("查找..."));
		edit.add(new JMenuItem("替换..."));
		edit.addSeparator();
		edit.add(new JMenuItem("复制"));
		edit.add(new JMenuItem("粘贴"));
		edit.add(new JMenuItem("剪切"));
		edit.addSeparator();
		edit.add(new JMenuItem("全选"));
		
		view.add(new JMenuItem("字体..."));
		view.add(item=new JMenuItem("颜色..."));
		item.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				Color c=JColorChooser.showDialog(frame, "请选择文字颜色", Color.BLACK);
				area.setForeground(c);
			}
			
		});
		
		JMenuBar bar=new JMenuBar();
		bar.add(file);
		bar.add(edit);
		bar.add(view);
		
		frame.setJMenuBar(bar);
	
	}
	
	private void init2(){
		JScrollPane jsp=new JScrollPane(area);
		frame.add(jsp,BorderLayout.CENTER);
	}
	
	
	public void showMe(){
		init();
		init2();
		frame.setSize(400,300);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public static void main(String[] arfs){
		new MenuTest().showMe();
	}

}
